﻿<?php require_once('../wp-blog-header.php'); ?>
<?php get_header(); ?>
<?php
require_once('conn.php');
// $username = $_SESSION['username'];
// $isLoginOK = $_SESSION['loginOK'];
// if(isset($_SESSION['loginOK']) && $_SESSION['loginOK'])
// {
	$mobileNO=$_POST['mobileNO'];
	$name=$_POST['name'];
	// var_dump($mobileNO);
	// var_dump($name);
	if (empty($mobileNO))
	{
		echo '<script language="JavaScript">window.alert("手机号码为空，提交失败，请重新输入。顾客未留手机号，请输入0");</script>'; 
		echo '<script language="JavaScript">window.location.href="inputForm.php";</script>'; 
	}
	elseif (empty($name))
	{
		echo '<script language="JavaScript">window.alert("姓名为空，提交失败，请重新输入。顾客未留姓名，请输入0");</script>'; 
		echo '<script language="JavaScript">window.location.href="inputForm.php";</script>'; 
	}	
	else
	{
		$sex=$_POST['sex']; 
		$score=$_POST['score']; 
		$rDomin=$_POST['rDomin']; 
		$rNaked=$_POST['rNaked']; 
		$rSph=$_POST['rSph']; 
		$rCyl=$_POST['rCyl']; 
		$rAxis=$_POST['rAxis']; 
		$rCorrect=$_POST['rCorrect']; 
		$rAdd=$_POST['rAdd']; 
		$rFH=$_POST['rFH']; 
		$rPD=$_POST['rPD']; 
		$lDomin=$_POST['lDomin']; 
		$lNaked=$_POST['lNaked']; 	
		$lSph=$_POST['lSph']; 
		$lCyl=$_POST['lCyl']; 
		$lAxis=$_POST['lAxis']; 
		$lCorrect=$_POST['lCorrect']; 	
		$lAdd=$_POST['lAdd']; 
		$lFH=$_POST['lFH']; 
		$lPD=$_POST['lPD']; 
		$buyDate=date("Y-m-d"); 
		$shopID=$_POST['shopID']; 
		$optID=$_POST['optID']; 
		$comments=$_POST['comments'];

		// 是否需要新建顾客
		$exec="select * from customer where mobileNO='".$mobileNO."'";
		$result = mysql_query($exec);
		$row = mysql_fetch_row($result);
		if ($row[0]=='')
		{
			$password=substr($mobileNO,-4);
			$exec="INSERT INTO customer(mobileNO, password, name, sex) values('$mobileNO', '$password', '$name', '$sex')";
			$result = mysql_query($exec);
		}
		// 加入购买记录
		$exec = "INSERT INTO buyRecord(mobileNO, name, sex, score, rDomin, rNaked, rSph, rCyl, rAxis, rCorrect, rAdd, rFH, rPD, lDomin, lNaked, lSph, lCyl, lAxis, lCorrect, lAdd, lFH, lPD, buyDate, shopID, optID, comments) VALUES('$mobileNO', '$name', '$sex', '$score', '$rDomin', '$rNaked', '$rSph', '$rCyl', '$rAxis', '$rCorrect', '$rAdd', '$rFH', '$rPD', '$lDomin', '$lNaked', '$lSph', '$lCyl', '$lAxis', '$lCorrect', '$lAdd', '$lFH', '$lPD', '$buyDate', '$shopID', '$optID', '$comments');";
		mysql_query("SET NAMES 'utf8'");
		if($result=mysql_query($exec))
		{					
			$query="SELECT LAST_INSERT_ID()";
			$result=mysql_query($query);
			$rows=mysql_fetch_row($result);
			$id_buyRecord = $rows[0];		
			// 创建取镜记录
			$exec="INSERT INTO takingRecord(id_buyRecord, reminderTimes, takingDate, optID) VALUES ('$id_buyRecord', 0, '0000-00-00', '');";
			//var_dump($exec);
			mysql_query($exec);
			
			$optMobile=18928487355;
			$carrierMobile=13147013570;
			
			//$optMobile=$mobileNO;
			//$carrierMobile=$mobileNO;			
			
			$query = "SELECT * FROM pairMap WHERE mapIndex='".$sex."'";
			$queryresult = mysql_query($query);
			$trans = mysql_fetch_object($queryresult);			
			$sex = $trans->mapName;			
			
			$url_header_dd="http://wt.chinaweimei.com.cn/apihttp/SMSSend.aspx?user=%B6%A5%B5%E7%D1%DB%BE%B5%D0%D0&pass=734yvu";
			$url_header_xz="http://wt.chinaweimei.com.cn/apihttp/SMSSend.aspx?user=%C9%EE%DB%DA%D6%EC%CE%B0%C9%FA&pass=weisheng";		
			
			$msg=$name.$sex."，感谢选择S-OPTA。新增".$score."积分。您的会员视力健康档案，访问s-opta.com，凭手机号及末4位登录查看";
			$msg=mb_convert_encoding($msg, 'gb2312' ,'utf-8');
			$url=$url_header_dd."&context=".$msg."&mobile=".$mobileNO;
			// var_dump($url);
			file_get_contents($url);
			
			$msg="【取镜时间】".$name.$sex."，取镜周期为间隔1个工作日，请留意短信通知。电话23601932投诉15818537286";
			$msg=mb_convert_encoding($msg, 'gb2312' ,'utf-8');
			$url=$url_header_dd."&context=".$msg."&mobile=".$mobileNO;
			file_get_contents($url);		
			
			$msg="【恭喜开单】天道酬勤，功不唐捐。让我们共同努力，S-OPTA明天会更好！".$buyDate;
			$msg=mb_convert_encoding($msg, 'gb2312' ,'utf-8');			
			$url=$url_header_dd."&context=".$msg."&mobile=".$optMobile;
			file_get_contents($url);
			
			$msg="【加工通知】有新订单需要加工。让我们共同努力，S-OPTA明天会更好！".$buyDate;
			$msg=mb_convert_encoding($msg, 'gb2312' ,'utf-8');	
			$url=$url_header_dd."&context=".$msg."&mobile=".$carrierMobile;
			file_get_contents($url);
			
			echo '<script language="JavaScript">window.alert("提交成功");</script>'; 
			echo '<script language="JavaScript">window.location.href="inputForm.php";</script>'; 
		}
		else
		{
			echo '<script language="JavaScript">window.alert("提交失败，请重新登录");</script>'; 
			echo '<script language="JavaScript">window.location.href="index.php";</script>'; 
		}	
	}

// }
// else
// {
	// // 验证失败，将 $_session["loginOK"] 置为 false 　　
	// $_SESSION['loginOK'] = false;
	// die("您无权访问"); 
// }
?>
<?php get_footer(); ?>